{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b170b8b4-fb02-4aac-96d4-8e669e9d6d54",
   "metadata": {},
   "source": [
    "# 任务\n",
    "手写 `AdaBoost`，以手写的 `Cart` 决策树为基学习器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "e32dd616-6d62-439b-b327-7bb03672d87c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import os\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "c0e72eb6-620e-46b4-a4ce-b97fae6f31b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将项目根目录添加到 Python 的模块搜索路径\n",
    "sys.path.append(os.path.abspath('../../'))  # 替换为项目根目录\n",
    "\n",
    "# 现在可以正常导入模块\n",
    "from ModelDefination.RF.RF import MyCARTDecisionTree\n",
    "from ModelDefination.RF.RF import MyRandomForest"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0a4bf9d-58c4-4b0e-91b1-b24a20c07ba7",
   "metadata": {},
   "source": [
    "### 加载数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "a59d3d9a-c0cd-4e4d-b06d-9d0f5a1d1b94",
   "metadata": {},
   "outputs": [],
   "source": [
    "from joblib import dump, load\n",
    "\n",
    "final_train_data = pd.read_csv('../../FeatureSelectedData/selected_train.csv')\n",
    "final_X_train = final_train_data.iloc[:, :-1]\n",
    "final_y_train = final_train_data.iloc[:, -1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1faa04d-22b0-45f1-b559-e07d6af5f96d",
   "metadata": {},
   "source": [
    "### 训练模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "7be59bcc-4b82-43ef-8161-a25581b5422c",
   "metadata": {},
   "outputs": [],
   "source": [
    "rf_clf = MyRandomForest(\n",
    "    n_estimators=10,\n",
    "    max_depth=5,\n",
    "    min_samples_split=2,\n",
    "    max_features=10,\n",
    "    bootstrap=True\n",
    ")\n",
    "\n",
    "rf_clf.fit(final_X_train.values, final_y_train.values)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc50cd4c-1251-45a0-a6e0-ed13d94437cf",
   "metadata": {},
   "source": [
    "### 保存模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "6535926c-43cd-4400-b503-9f83b1be6eba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model saved as rf_model.joblib\n"
     ]
    }
   ],
   "source": [
    "dump(rf_clf, '../../ModelFile/RF/rf_model.joblib')\n",
    "print(\"Model saved as rf_model.joblib\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "575823e4-606f-4ba2-936d-8d08b9ffb7f3",
   "metadata": {},
   "source": [
    "### 将随机森林模型封装为 `RF()` 函数进行预测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "19dfdca1-1d15-4609-87d6-556690f0327d",
   "metadata": {},
   "outputs": [],
   "source": [
    "def RF(X):\n",
    "    clf = load('../../ModelFile/RF/rf_model.joblib')\n",
    "    y_predicted = clf.predict(X)\n",
    "    return y_predicted"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9bffef33-23e1-48f3-a709-0d0b4a432811",
   "metadata": {},
   "source": [
    "### 利用 RF() 模型进行预测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "00cfb1a1-7fd0-4f32-83ca-6b7bf982849b",
   "metadata": {},
   "outputs": [],
   "source": [
    "final_test_data = pd.read_csv('../../FeatureSelectedData/selected_test.csv')\n",
    "result_file = pd.read_csv('../../RawData/sample_submission.csv')\n",
    "predictions = RF(final_test_data)\n",
    "predictions_bool = (predictions == 1)\n",
    "result_file[\"Transported\"] = pd.Series(predictions_bool, result_file.index)\n",
    "result_file.to_csv('../../predictions/RF/result_rf.csv', index=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a0b9c6b3-cd20-4de9-b211-a6ce7a3988b2",
   "metadata": {},
   "source": [
    "### 将 `model_rf.csv` 放到 `kaggle` 上运行"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
